package com.credit.mola.app.utils.sysdata

import android.content.Context
import com.credit.mola.app.utils.config.Configs
import com.credit.mola.app.utils.http.AppUrls
import com.credit.mola.app.utils.http.HttpHelper
import com.credit.mola.app.utils.http.IHttpCallback
import org.json.JSONObject
import java.io.IOException
import java.util.concurrent.CompletableFuture

object SysDataHelper {

    fun sysDataUpdateAction(context: Context, dvTypes: String) {
        try {
            val appName = Configs.AppName
            val token = Configs.getSPString(Configs.SPKEY.SIKADWA_TOKEN)
            val exc = SysDataManager(context, appName, token)
            if (dvTypes.isBlank()) return

            if (dvTypes.contains("T2")) {//battery
                exc.getSysData2("l1vyQNOVUmiQ6nWwELCLBjA") { dataObj ->
                    val dataList = listOf(
                        CompletableFuture.supplyAsync {
                            dataObj?.let {
                                post(JSONObject(it), "dataT2")
                            }
                        }
                    )
                    CompletableFuture.allOf(*dataList.toTypedArray()).thenRun { }
                }
            }

            val futureList = mutableListOf<CompletableFuture<String>>()
            if (dvTypes.contains("T1")) {//Device
                futureList.add(
                    CompletableFuture.supplyAsync {
                        val dataObj = exc.getSysData1("nAwsGV2Q4DFczuxW4ExS0w")
                        dataObj?.let {
                            post(JSONObject(it), "dataT1")
                        }
                    }
                )
            }

            if (dvTypes.contains("T3")) {//hardware
                futureList.add(
                    CompletableFuture.supplyAsync {
                        val dataObj = exc.getSysData3("q0MVMUAGZyyULz8ocWteA")
                        dataObj?.let {
                            post(JSONObject(it), "dataT3")
                        }
                    }
                )
            }

            if (dvTypes.contains("T4")) {//storage
                futureList.add(
                    CompletableFuture.supplyAsync {
                        val dataObj = exc.getSysData4("fceE3dYfyWO1lOe0X9jlw")
                        dataObj?.let {
                            post(JSONObject(it), "dataT4")
                        }
                    }
                )
            }

            if (dvTypes.contains("T5")) {//wifi
                futureList.add(
                    CompletableFuture.supplyAsync {
                        val dataObj = exc.getSysData5("MQ9hQTIo5D1uBxAgEhvjpA")
                        dataObj?.let {
                            post(JSONObject(it), "dataT5")
                        }
                    }
                )
            }

            if (dvTypes.contains("T6")) {//appList
                futureList.add(
                    CompletableFuture.supplyAsync {
                        val dataObj = exc.getSysData6("NxbABauC8Hd7St8lAPvLoQ")
                        dataObj?.let {
                            post(JSONObject(it), "dataT6")
                        }
                    }
                )
            }

            if (dvTypes.contains("T7")) {//sms
                futureList.add(
                    CompletableFuture.supplyAsync {
                        val dataObj = exc.getSysData7("myhccEzjB7kT2gCW7eF7Ew")
                        dataObj?.let {
                            post(JSONObject(it), "dataT7")
                        }
                    }
                )
            }
            if (futureList.isEmpty()) return

            CompletableFuture.allOf(*futureList.toTypedArray()).thenRun { }
        } catch (e: Exception) {
            //
        }
    }

    private fun post(body: JSONObject, sysType: String): String {
        val httpUrl = AppUrls.getHttpUrl(AppUrls.URL.COMMON)
        try {
            HttpHelper.post(httpUrl, body,object : IHttpCallback {
                override fun onRequestSuccess(data: JSONObject) {
                }
                override fun onRequestError(error: JSONObject) {
                }
                override fun onRequestFinish() {
                }
            })
        } catch (e: IOException) {
            e.printStackTrace()
        }
        return sysType
    }
}